﻿using System;
using System.Windows.Forms;
using System.Xml;
using CommonFunction;

namespace Robot_Roulettist_V9
{
    public partial class FrmConfiguration : Form
    {
        public FrmConfiguration()
        {
            InitializeComponent();
        }

        private void FrmConfiguration_Load(object sender, EventArgs e)
        {
            
        }

        private void cboFollowingStrategy_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadFollowingStrategy((Common.STRATEGIES)cboFollowingStrategy.SelectedIndex);
        }

        private void cboTypeOfStrategy_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadOtherStrategy((Common.STRATEGIES)cboTypeOfStrategy.SelectedIndex);
        }

        private void chkStrategy_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;

            if (chk.Name == chkOtherStrategy.Name)
            {
                chkFollowing.Checked = !chk.Checked;
                grpFollowing.Enabled = chkFollowing.Checked;
            }
            else
            {
                chkOtherStrategy.Checked = !chk.Checked;
                grpOtherStrategy.Enabled = chkOtherStrategy.Checked;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Updated()) MessageBox.Show("The data saved successfully", "Saving data configuration", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            var confirm = MessageBox.Show("You want to reset data ?", "Data Configuration confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (confirm == System.Windows.Forms.DialogResult.OK)
            {
                LoadConfigData();
                LoadFollowingStrategy((Common.STRATEGIES)cboFollowingStrategy.SelectedIndex);
                LoadOtherStrategy((Common.STRATEGIES)cboTypeOfStrategy.SelectedIndex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region "Load data configuration"

        public bool InitForm()
        {
            cboBetRate.SelectedIndex = 0;
            cboTypeOfStrategy.SelectedIndex = 0;
            cboWaiting.SelectedIndex = 0;
            cboFollowingStrategy.SelectedIndex = 0;

            LoadOtherStrategy(Common.STRATEGIES.BLACK_RED);
            LoadFollowingStrategy(Common.STRATEGIES.BLACK_RED);

            LoadConfigData();

            return true;
        }

        private void LoadConfigData()
        {
            AccessData objData = null;
            XmlDocument xmlData = null;

            try
            {
                objData = new AccessData();
                xmlData = objData.GetData();

                cboBetRate.Text = xmlData.GetBetRate();
                txtStopWinning.Text = xmlData.GetStopWinning();
                txtStopLosing.Text = xmlData.GetStopLosing();

                chkFollowing.Checked = (xmlData.GetDoItFollowing() == "True");
                grpFollowing.Enabled = chkFollowing.Checked;
                chkOtherStrategy.Checked = !chkFollowing.Checked;
                grpOtherStrategy.Enabled = chkOtherStrategy.Checked;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Loading data configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                xmlData = null;
                objData = null;
            }
        }

        private void LoadFollowingStrategy(Common.STRATEGIES strategy)
        {
            AccessData objData = null;
            XmlDocument xmlData = null;

            try
            {
                objData = new AccessData();
                xmlData = objData.GetData();


                XmlNode strategyData = null;
                string ctrlName = string.Empty;
                string nodeName = string.Empty;
                string keyNode = string.Empty;

                switch (strategy)
                {
                    case Common.STRATEGIES.BLACK_RED:
                        strategyData = xmlData.GetFollowingBlackRedValues();
                        keyNode = "BR";
                        break;

                    case Common.STRATEGIES.EVEN_ODD:
                        strategyData = xmlData.GetFollowingEvenOddValues();
                        keyNode = "EO";
                        break;

                    case Common.STRATEGIES.HIGH_LOW:
                        strategyData = xmlData.GetFollowingHighLowValues();
                        keyNode = "HL";
                        break;

                    default: return;
                }

                for (int i = 1; i < 31; i++)
                {
                    ctrlName = string.Format("txtF{0}", i.ToString("00"));                    
                    nodeName = string.Format("{0}{1}", keyNode, i.ToString("00"));
                    grpFollowing.Controls[ctrlName].Text = strategyData[nodeName].InnerText;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Loading data configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                xmlData = null;
                objData = null;
            }
        }

        private void LoadOtherStrategy(Common.STRATEGIES strategy)
        {
            AccessData objData = null;
            XmlDocument xmlData = null;

            try
            {
                objData = new AccessData();
                xmlData = objData.GetData();


                XmlNode strategyData = null;
                string ctrlName = string.Empty;
                string nodeName = string.Empty;
                string keyNode = string.Empty;

                switch (strategy)
                {
                    case Common.STRATEGIES.BLACK_RED:
                        strategyData = xmlData.GetBlackRedValues();
                        keyNode = "BR";
                        break;

                    case Common.STRATEGIES.EVEN_ODD:
                        strategyData = xmlData.GetEvenOddValues();
                        keyNode = "EO";
                        break;

                    case Common.STRATEGIES.HIGH_LOW:
                        strategyData = xmlData.GetHighLowValues();
                        keyNode = "HL";
                        break;

                    case Common.STRATEGIES.DOZENS:
                        strategyData = xmlData.GetDozenValues();
                        keyNode = "Dozen";
                        break;

                    case Common.STRATEGIES.COLUMNS:
                        strategyData = xmlData.GetColumnValues();
                        keyNode = "CL";
                        break;

                    case Common.STRATEGIES.NUMBERS:
                        strategyData = xmlData.GetNumberValues();
                        keyNode = "NO";
                        break;

                    default: return;
                }

                cboWaiting.Text = strategyData["Wait"].InnerText;

                for (int i = 1; i < 31; i++)
                {
                    ctrlName = string.Format("txtO{0}", i.ToString("00"));
                    nodeName = string.Format("{0}{1}", keyNode, i.ToString("00"));
                    grpOtherStrategy.Controls[ctrlName].Text = strategyData[nodeName].InnerText;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Loading data configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                xmlData = null;
                objData = null;
            }
        }

        #endregion

        #region "Update data configuration"

        private bool Updated()
        {
            AccessData objData = null;
            XmlDocument xmlData = null;
            bool success = false;

            try
            {
                objData = new AccessData();
                xmlData = objData.GetData();

                xmlData.SetBetRate(cboBetRate.Text);
                if (string.IsNullOrEmpty(txtStopWinning.Text.Trim())) txtStopWinning.Text = "0";
                xmlData.SetStopWinning(txtStopWinning.Text);
                if (string.IsNullOrEmpty(txtStopLosing.Text.Trim())) txtStopLosing.Text = "0";
                xmlData.SetStopLosing(txtStopLosing.Text);

                xmlData.SetDoItFollowing(chkFollowing.Checked.ToString());

                //following strategy
                string ctrlName = string.Empty;
                var strategy = (Common.STRATEGIES)cboFollowingStrategy.SelectedIndex;
                string[] followingValues = new string[30];

                for (int i = 1; i < 31; i++)
                {
                    ctrlName = string.Format("txtF{0}", i.ToString("00"));
                    followingValues[i - 1] = grpFollowing.Controls[ctrlName].Text;
                }
                switch (strategy)
                {
                    case Common.STRATEGIES.BLACK_RED:
                        xmlData.SetFollowingBlackRedValues(followingValues);
                        break;

                    case Common.STRATEGIES.EVEN_ODD:
                        xmlData.SetFollowingEvenOddValues(followingValues);
                        break;

                    case Common.STRATEGIES.HIGH_LOW:
                        xmlData.SetFollowingHighLowValues(followingValues);
                        break;

                    default: break;
                }

                //other strategy
                strategy = (Common.STRATEGIES)cboTypeOfStrategy.SelectedIndex;
                string[] otherValues = new string[31];

                otherValues[0] = cboWaiting.Text;
                for (int i = 1; i < 31; i++)
                {
                    ctrlName = string.Format("txtO{0}", i.ToString("00"));
                    otherValues[i] = grpOtherStrategy.Controls[ctrlName].Text;
                }
                switch (strategy)
                {
                    case Common.STRATEGIES.BLACK_RED:
                        xmlData.SetBlackRedValues(otherValues);
                        break;

                    case Common.STRATEGIES.EVEN_ODD:
                        xmlData.SetEvenOddValues(otherValues);                        
                        break;

                    case Common.STRATEGIES.HIGH_LOW:
                        xmlData.SetHighLowValues(otherValues);
                        break;

                    case Common.STRATEGIES.DOZENS:
                        xmlData.SetDozenValues(otherValues);
                        break;

                    case Common.STRATEGIES.COLUMNS:
                        xmlData.SetColumnValues(otherValues);
                        break;

                    case Common.STRATEGIES.NUMBERS:
                        xmlData.SetNumberValues(otherValues);
                        break;

                    default: break;
                }

                objData.SaveData(xmlData);

                success = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Loading data configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                xmlData = null;
                objData = null;
            }

            return success;
        }

        #endregion
    }
}
